﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Drawing.Imaging;

namespace Send
{
    public partial class Form1 : Form
    {
        static Stream fs = null;
        int nameLen = 0;
        List<byte> name;
        long contentLen = 0;
        List<byte> content;

        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        private static extern bool BitBlt(
        IntPtr hdcDest, //目的DC的句柄 
        int nXDest, //目的图形的左上角的x坐标 
        int nYDest, //目的图形的左上角的y坐标 
        int nWidth, //目的图形的矩形宽度 
        int nHeight, //目的图形的矩形高度 
        IntPtr hdcSrc, //源DC的句柄 
        int nXSrc, //源图形的左上角的x坐标 
        int nYSrc, //源图形的左上角的x坐标 
        System.Int32 dwRop //光栅操作代码 
        );
        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        private static extern IntPtr CreateDC(
           string lpszDriver,
           string lpszDevice,
           string lpszOutput,
           IntPtr lpInitData
           );

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtIP.Text.Trim()))
            {
                MessageBox.Show("请输入IP地址:"); return;
            }
            //实例化一个基于TCP/IP的基于流的套接字
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            EndPoint endPoint = new IPEndPoint(IPAddress.Parse(txtIP.Text), 8080);
            sock.Connect(endPoint);
            //组建数据包
            //文件长度
            contentLen = fs.Length;
            //文件内容         
            sock.Send(BitConverter.GetBytes(contentLen));
            //循环发送文件内容
            while (true)
            {
                byte[] bits = new byte[256];
                int r = fs.Read(bits, 0, bits.Length);
                if (r <= 0) break; //如果从流中读取完毕,则break;
                sock.Send(bits, r, SocketFlags.None);
            }
            sock.Close();
            //由于读取操作会是文件指针产生偏移,最后读取结束之后,要将指针置为0;
            fs.Position = 0;
        }

        private void btnPicCatch_Click(object sender, EventArgs e)
        {
            //获取主屏
            Screen s = Screen.PrimaryScreen;
            //创建一个位图,将其大小设置为何屏幕大小一致,为了获取屏幕的图片
            Bitmap bit = new Bitmap(s.Bounds.Width, s.Bounds.Height);
            //利用当前bit获取一个画布,画布已经于Graphics对象关联
            Graphics g = Graphics.FromImage(bit);
            //将屏幕的(0,0)坐标截图内容copy到画布的(0,0)位置,尺寸到校 bit.size;
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), bit.Size);
            //将位图保存到D盘
            bit.Save("D:\\123.jpg");
            //释放位图资源
            bit.Dispose();
            //释放画布
            g.Dispose();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "只能发送图片文件|*.png;*.jpg;*.gif|All Files|*.*";
            if (DialogResult.OK == dlg.ShowDialog())
            {
                fs = dlg.OpenFile();
                txtPath.Text = dlg.FileName;
                //文件名长度
                nameLen = Path.GetFileName(dlg.FileName).Length;
                //文件名内容
                Encoding encoding = new UTF8Encoding();
                //将文件名转换为字节
                name = encoding.GetBytes(Path.GetFileName(dlg.FileName)).ToList();
            }
        }

        private void btnPicPart_Click(object sender, EventArgs e)
        {
            IntPtr dcScreen = CreateDC("DISPLAY", null, null, (IntPtr)null);
            Graphics g1 = Graphics.FromHdc(dcScreen);//获得桌面对象 
            int tmpX = Convert.ToInt32(txtX.Text);
            int tmpY = Convert.ToInt32(txtY.Text);
            int tmpWidth = Convert.ToInt32(txtWidth.Text);
            int tmpHeight = Convert.ToInt32(txtHeight.Text);
            int tmpRop = tmpWidth * tmpHeight;
            Image MyImage = new Bitmap(tmpWidth, tmpHeight, g1);
            Graphics g2 = Graphics.FromImage(MyImage); //创建位图图形对象
            IntPtr dc1 = g1.GetHdc();//获得窗体的上下文设备
            IntPtr dc2 = g2.GetHdc();//获得位图文件的上下文设备
            BitBlt(dc2, 0, 0, tmpWidth, tmpHeight, dc1, tmpX, tmpY, 13369376); //写入到位图
            g1.ReleaseHdc(dc1); //释放窗体的上下文设备 
            g2.ReleaseHdc(dc2); //释放位图文件的上下文设备 
            //保存为jpeg文件
            MyImage.Save("D:\\121.jpg", ImageFormat.Jpeg);//保存为jpeg文件 
            
            MessageBox.Show("保存图片结束！"); 
        }

        

        
    }
}
